<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title> Slices </title>

  <link rel="stylesheet" media="screen" href="/css/style.css" >
</head>
<body>

  <header class="site-header">
    <h3 class="site-title">
      <a href="https://mkaz.github.io/working-with-go/">
        Working with Go
      </a>
    </h3>
  </header>

  <main>

    <aside>
      <h2>Contents</h2>
      <ul>
        
          <li><a href="/hello.html">Hola Mundo</a></li>
        
          <li><a href="/strings.html">Strings</a></li>
        
          <li><a href="/slices.html">Slices</a></li>
        
          <li><a href="/functions.html">Functions</a></li>
        
          <li><a href="/control-structures.html">Control Structures</a></li>
        
          <li><a href="/for-loops.html">For Loops</a></li>
        
          <li><a href="/structs.html">Structs</a></li>
        
          <li><a href="/maps.html">Maps</a></li>
        
          <li><a href="/files.html">Files</a></li>
        
          <li><a href="/read-dir.html">Directories</a></li>
        
          <li><a href="/objects.html">Objects</a></li>
        
          <li><a href="/command-line.html">Command-line</a></li>
        
          <li><a href="/date-time.html">Date/Time</a></li>
        
          <li><a href="/sorting.html">Sorting</a></li>
        
          <li><a href="/json.html">JSON</a></li>
        
          <li><a href="/web-server.html">Web Server</a></li>
        
          <li><a href="/guess-game.html">Guessing Game</a></li>
        
          <li><a href="/rest-api.html">REST API</a></li>
        
          <li><a href="/regexps.html">Regexp</a></li>
        
          <li><a href="/memcache.html">Memcache</a></li>
        
          <li><a href="/mysql.html">MySQL</a></li>
        
          <li><a href="/interfaces.html">Interfaces</a></li>
        
          <li><a href="/testing.html">Testing</a></li>
        
      </ul>
    </aside>

    <article class="entry-content">
      <h1>Working with Slices</h1>

<p>Go has an array type, but it has a fixed size. Slices in Go are dynamically-sized and more flexible. Slices are the common data type you will work with.</p>

<p>The specification for slices are <code>[]T</code> where T is the type, for example <code>[]string</code> is a set of strings, or <code>[]int</code> is a set of integers.</p>

<h2>Initialize</h2>

<p>Initialize an empty slice.</p>

<pre><code class="language-go">var empty []int
</code></pre>

<p>Initialize a slice with values.</p>

<pre><code class="language-go">alphas := []string{&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;, &quot;jkl&quot;}
</code></pre>

<h2>Append</h2>

<p>Slices can not be modified, a new copy needs to be made, it is common to copy to the same variable.</p>

<pre><code class="language-go">var nums []int
nums = append(nums, 203)
nums = append(nums, 302)
fmt.Println(nums)
// [203 302]
</code></pre>

<p>Append multiple values at once.</p>

<pre><code class="language-go">alphas := []string{&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;, &quot;jkl&quot;}
alphas = append(alphas, &quot;mno&quot;, &quot;pqr&quot;, &quot;stu&quot;)
</code></pre>

<h2>Common Array Operations</h2>

<p>Get length of a slice.</p>

<pre><code class="language-go">fmt.Println(&quot;Length: &quot;, len(alphas))
</code></pre>

<p>Retrieve a single element from slice</p>

<pre><code class="language-go">fmt.Println(alphas[1])
</code></pre>

<p>Retrieve a slice of a slice</p>

<pre><code class="language-go">alpha2 := alphas[1:3]
fmt.Println(alpha2)
</code></pre>

<h2>Element Exists in a Slice</h2>

<p>There is no built-in function to determine if an element exists in a slice. Here is an example function to check if a string exists in an array of strings.</p>

<pre><code class="language-go">package main

import &quot;fmt&quot;

func main() {

	alphas := []string{&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;, &quot;jkl&quot;}

	if elemExists(&quot;def&quot;, alphas) {
		fmt.Println(&quot;Exists!&quot;)
	}
}

func elemExists(s string, a []string) bool {
	for _, v := range a {
		if v == s {
			return true
		}
	}
	return false
}
</code></pre>

    </article>

  </main>

<footer class="site-footer" role="contentinfo">
    An <a href="https://mkaz.blog/">mkaz</a> joint
    <span class="sep"> | </span>
    <a href="https://github.com/mkaz/hasite">Powered by Hastie</a>
</footer>

<script src="/js/asciinema/asciinema-player.js"></script>
<script src="/js/prism.js"></script>

</body>
</html>